package GUI.lecturerGUI;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;

import GUI.MainGUI;

import controllers.LecturerController;
import entities.*;

import javax.swing.JSlider;
import common.Perror;

public class LecturerExam extends JPanel implements ActionListener,
		ChangeListener, FocusListener, KeyListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private LecturerController lecturerCtrl;
	private ArrayList<Subject> subjectLst;
	private ArrayList<Course> courseLst;
	private List<Question> questionLst;
	private ArrayList<QuestionPerExam> examQuestions;

	private JSlider sldExamTime;

	private JComboBox cboxQstSelect;// TODO JComboBox<String>
	private JComboBox cboxSubject;// TODO JComboBox<String>
	private JComboBox cboxCourse;// TODO JComboBox<String>

	private JTextField txtTotalPoints;
	private JTextField txtQCount;
	private JTextField txtExamNumber;
	private JTextField txtPoint;

	private JTextArea txtQstPreview;
	private JTextArea txtLecturerComments;
	private JTextArea txtStudentComments;

	private JButton btnMain;
	private JButton btnExit;
	private JButton btnAddQuestion;
	private JButton btnCreateNewExam;

	private JLabel lblCreateNewExam;
	private JLabel lblEnterExamSubject;
	private JLabel lblEnterExam;
	private JLabel lblChooseQuestion;
	private JLabel lblQuestionPoints;
	private JLabel lblEnterExamTime;
	private JLabel lblCommentsForStudent;
	private JLabel lblCommentsForLecturer;
	private JLabel lblExamTotalPoint;
	private JLabel lblQuestionNum;
	private JLabel lblExamTime;
/**
 * the lecturer create new exam to the DB 
 * @param lecturerCtrl the lecturer Controller 
 */
	public LecturerExam(LecturerController lecturerCtrl) {

		super();
		this.lecturerCtrl = lecturerCtrl;
		initialize();
		if (examQuestions == null)
			examQuestions = new ArrayList<QuestionPerExam>();
	}
	/**
	 * initializing the GUI
	 */
	private void initialize() {
		setBorder(new TitledBorder(null, "Lecturer  Exam: ",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(SystemColor.activeCaption);
		setBounds(0, 0, MainGUI.setX, MainGUI.setY);
		setLayout(null);
		add(getExit());
		add(GETbtnMain());
		add(GETlblCreateNewExam());
		add(GETlblEnterExamSubject());
		add(GETtxtSubject());
		add(GETlblEnterExam());
		add(GETtxtCourse());
		add(GETtxtExamNumber());
		add(GETbtnAddQuestion());
		add(GETcboxChooseQuestion());
		add(GETlblChooseQuestion());
		add(GETlblQuestionPoints());
		add(GETlblEnterExamTime());
		add(GETtxtPoint());
		add(GETbtnCreateNewExam());
		add(GETlblCommentsForStudent());
		add(GETtxtrStudentComments());
		add(GETtxtrLecturerComments());
		add(GETlblCommentsForLecturer());
		add(GETlblExamTotalPoint());
		add(GETlblQuestionNum());
		add(GETtxtQstPreview());
		add(GETcboxSubject());
		add(GETcboxCourse());
		add(GETsldExamTime());
		add(GETlblExamTime());
	}

	private JLabel GETlblExamTime() {
		if (lblExamTime == null) {
			lblExamTime = new JLabel("180");
			lblExamTime.setBounds(369, 416, 46, 14);
		}
		return lblExamTime;
	}

	private JSlider GETsldExamTime() {
		if (sldExamTime == null) {
			sldExamTime = new JSlider();
			sldExamTime.setMinorTickSpacing(10);
			sldExamTime.setMajorTickSpacing(30);
			sldExamTime.setPaintTicks(true);
			sldExamTime.setValue(180);
			sldExamTime.setMaximum(240);
			sldExamTime.setMinimum(60);
			sldExamTime.setBounds(295, 432, 200, 23);
			sldExamTime.addChangeListener(this);
		}
		return sldExamTime;
	}

	private JComboBox GETcboxCourse() {// TODO JComboBox<String>
		if (cboxCourse == null) {
			cboxCourse = new JComboBox();// TODO JComboBox<String>
			cboxCourse.setEnabled(false);
			cboxCourse.setBounds(320, 45, 122, 20);
		}
		return cboxCourse;
	}

	private JComboBox GETcboxSubject() {// TODO JComboBox<String>
		if (cboxSubject == null) {
			cboxSubject = new JComboBox();// TODO JComboBox<String>
			cboxSubject.setBounds(107, 48, 122, 20);

		}
		return cboxSubject;
	}

	private JTextArea GETtxtQstPreview() {
		if (txtQstPreview == null) {
			txtQstPreview = new JTextArea();
			txtQstPreview.setEnabled(false);
			txtQstPreview.setBounds(140, 86, 350, 153);
			txtQstPreview.setColumns(10);
		}
		return txtQstPreview;
	}

	private JButton GETbtnMain() {
		if (btnMain == null) {
			btnMain = new JButton("Main Menu");
			btnMain.setBounds(22, 466, 110, 23);
			btnMain.addActionListener(this);
		}
		return btnMain;
	}

	private JLabel GETlblCreateNewExam() {
		if (lblCreateNewExam == null) {
			lblCreateNewExam = new JLabel("Create new exam :");
			lblCreateNewExam.setHorizontalAlignment(SwingConstants.CENTER);
			lblCreateNewExam.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblCreateNewExam.setBackground(Color.ORANGE);
			lblCreateNewExam.setBounds(97, 11, 150, 23);

		}
		return lblCreateNewExam;
	}

	private JLabel GETlblEnterExamSubject() {
		if (lblEnterExamSubject == null) {
			lblEnterExamSubject = new JLabel("Exam subject:");
			lblEnterExamSubject.setBounds(10, 41, 100, 23);
		}
		return lblEnterExamSubject;
	}

	private JTextField GETtxtSubject() {
		if (txtTotalPoints == null) {
			txtTotalPoints = new JTextField();
			txtTotalPoints.setText("0");
			txtTotalPoints.setEnabled(false);
			txtTotalPoints.setBounds(84, 435, 48, 20);
			txtTotalPoints.setColumns(10);
			txtTotalPoints.addActionListener(this);

		}
		return txtTotalPoints;
	}

	private JLabel GETlblEnterExam() {
		if (lblEnterExam == null) {
			lblEnterExam = new JLabel("Exam course:");
			lblEnterExam.setBounds(237, 48, 100, 14);

		}
		return lblEnterExam;
	}

	private JTextField GETtxtCourse() {
		if (txtQCount == null) {
			txtQCount = new JTextField();
			txtQCount.setText("0");
			txtQCount.setEnabled(false);
			txtQCount.setBounds(237, 435, 48, 20);
			txtQCount.setColumns(10);
			txtQCount.addActionListener(this);
		}
		return txtQCount;
	}

	private JTextField GETtxtExamNumber() {
		if (txtExamNumber == null) {
			txtExamNumber = new JTextField();
			txtExamNumber.setEnabled(false);
			txtExamNumber.setText("0");
			txtExamNumber.setBounds(257, 14, 113, 20);
			txtExamNumber.setColumns(10);
			txtExamNumber.addActionListener(this);
		}
		return txtExamNumber;
	}

	private JButton GETbtnAddQuestion() {
		if (btnAddQuestion == null) {
			btnAddQuestion = new JButton("Add question");
			btnAddQuestion.setEnabled(false);
			btnAddQuestion.setBounds(10, 190, 122, 38);
			btnAddQuestion.addActionListener(this);
		}
		return btnAddQuestion;
	}

	private JComboBox GETcboxChooseQuestion() {// TODO JComboBox<String>
		if (cboxQstSelect == null) {
			cboxQstSelect = new JComboBox();// TODO JComboBox<String>
			cboxQstSelect.setEnabled(false);
			cboxQstSelect.setBounds(10, 135, 122, 23);
		}
		return cboxQstSelect;
	}

	private JLabel GETlblChooseQuestion() {
		if (lblChooseQuestion == null) {
			lblChooseQuestion = new JLabel("Choose question:");
			lblChooseQuestion.setBounds(10, 110, 122, 14);
		}
		return lblChooseQuestion;
	}

	private JLabel GETlblQuestionPoints() {
		if (lblQuestionPoints == null) {
			lblQuestionPoints = new JLabel("Question points:");
			lblQuestionPoints.setBounds(10, 165, 100, 14);
		}
		return lblQuestionPoints;
	}

	private JLabel GETlblEnterExamTime() {
		if (lblEnterExamTime == null) {
			lblEnterExamTime = new JLabel("Exam time:");
			lblEnterExamTime.setBounds(297, 416, 75, 14);
		}
		return lblEnterExamTime;
	}

	private JTextField GETtxtPoint() {
		if (txtPoint == null) {
			txtPoint = new JTextField();
			txtPoint.setText("0");
			txtPoint.setBounds(104, 162, 27, 20);
			txtPoint.setColumns(10);
			txtPoint.setEnabled(false);
			txtPoint.addKeyListener(this);
		}
		return txtPoint;
	}

	private JButton GETbtnCreateNewExam() {
		if (btnCreateNewExam == null) {
			btnCreateNewExam = new JButton("Create new exam");
			btnCreateNewExam.setEnabled(false);
			btnCreateNewExam.setBounds(340, 466, 150, 23);
			btnCreateNewExam.addActionListener(this);
		}
		return btnCreateNewExam;
	}

	private JLabel GETlblCommentsForStudent() {
		if (lblCommentsForStudent == null) {
			lblCommentsForStudent = new JLabel("Comments for student:");
			lblCommentsForStudent.setBounds(10, 250, 141, 14);
		}
		return lblCommentsForStudent;
	}

	private JTextArea GETtxtrStudentComments() {
		if (txtStudentComments == null) {
			txtStudentComments = new JTextArea();
			txtStudentComments.setText("Student comments");
			txtStudentComments.setBounds(20, 275, 350, 45);
			txtStudentComments.addFocusListener(this);

		}
		return txtStudentComments;
	}

	private JLabel GETlblCommentsForLecturer() {
		if (lblCommentsForLecturer == null) {
			lblCommentsForLecturer = new JLabel("Comments for lecturer:");
			lblCommentsForLecturer.setBounds(10, 335, 141, 14);
		}
		return lblCommentsForLecturer;
	}

	private JTextArea GETtxtrLecturerComments() {
		if (txtLecturerComments == null) {
			txtLecturerComments = new JTextArea();
			txtLecturerComments.setText("Lecturer comments");
			txtLecturerComments.setBounds(22, 360, 350, 45);
			txtLecturerComments.addFocusListener(this);
		}
		return txtLecturerComments;
	}

	private JLabel GETlblExamTotalPoint() {
		if (lblExamTotalPoint == null) {
			lblExamTotalPoint = new JLabel("Total points:");
			lblExamTotalPoint.setBounds(21, 416, 129, 14);
		}
		return lblExamTotalPoint;
	}

	private JLabel GETlblQuestionNum() {
		if (lblQuestionNum == null) {
			lblQuestionNum = new JLabel("Number of question :");
			lblQuestionNum.setBounds(140, 416, 129, 14);
		}
		return lblQuestionNum;
	}

	/**
	 * @return the examQuestions
	 */
	public ArrayList<QuestionPerExam> getExamQuestions() {

		return new ArrayList<QuestionPerExam>(examQuestions);
	}

	/**
	 * @param examQuestions
	 *            the examQuestions to set
	 */
	public void setExamQuestions(ArrayList<QuestionPerExam> examQuestions) {
		this.examQuestions = examQuestions;
	}

	private JButton getExit() {
		if (btnExit == null) {
			btnExit = new JButton("EXIT");
			btnExit.setBounds(133, 466, 110, 23);
			btnExit.addActionListener(this);
		}
		return btnExit;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == btnExit) {
			RestoreToDefualt();
			lecturerCtrl.handleLectutereGUI(LecturerController.EXIT);
		}

		if (e.getSource() == btnMain) {
			RestoreToDefualt();
			lecturerCtrl.handleLectutereGUI(LecturerController.MAIN);
		}

		if (e.getSource() == btnAddQuestion) {
			Integer totalPoints = new Integer(Integer.parseInt(txtTotalPoints
					.getText()) + Integer.parseInt(txtPoint.getText()));
				int selectedQ = cboxQstSelect.getSelectedIndex();
				cboxSubject.setEnabled(false);
				QuestionPerExam newQ = new QuestionPerExam(
						Integer.parseInt(txtPoint.getText()),
						questionLst.get(selectedQ));
				examQuestions.add(newQ);
				cboxQstSelect.removeItemAt(selectedQ);
				questionLst.remove(selectedQ);
				txtTotalPoints.setText(totalPoints.toString());
				Integer numOfQ = new Integer(examQuestions.size());
				txtQCount.setText(numOfQ.toString());
				txtPoint.setText("0");
				btnAddQuestion.setEnabled(false);

		}

		if (e.getSource() == btnCreateNewExam)
			lecturerCtrl.createNewExam();

		if (e.getSource() == cboxSubject) {
			if ((subjectLst != null) || (!subjectLst.isEmpty())) {
				cboxCourse.removeAllItems();
				cboxQstSelect.removeAllItems();
				int comboindex = cboxSubject.getSelectedIndex();
				if ((comboindex < subjectLst.size()) && (comboindex >= 0)) {
					Subject selectedSubject = subjectLst.get(comboindex);
					lecturerCtrl.setCoursesPerSubject(selectedSubject);
					lecturerCtrl.setQuestionsPerSubject(selectedSubject);
					enableTxts();
				}
			}
		}
		if (e.getSource() == cboxQstSelect) {
			if ((subjectLst != null) || (!subjectLst.isEmpty())) {
				int comboindex = cboxQstSelect.getSelectedIndex();
				if ((comboindex < questionLst.size()) && (comboindex >= 0)) {
					txtQstPreview.setText("");
					txtQstPreview
							.append(questionLst.get(comboindex).toString());
				}
			}

		}

		if (e.getSource() == cboxCourse) {
			if ((courseLst != null) || (!courseLst.isEmpty())) {
				Integer qnum;
				int sID = -1;
				int cID = -1;
				int courseComboindex = cboxCourse.getSelectedIndex();
				int subjectComboindex = cboxSubject.getSelectedIndex();
				if ((subjectComboindex < subjectLst.size())
						&& (subjectComboindex >= 0)) {

					Subject selectedSubject = subjectLst.get(subjectComboindex);
					if ((courseComboindex < courseLst.size())
							&& (courseComboindex >= 0)) {
						Course selectedCourse = courseLst.get(courseComboindex);
						sID = selectedSubject.getSubjectID();
						cID = selectedCourse.getCourseID();
						qnum = new Integer(lecturerCtrl.getNewExamID(cID, sID));
						txtExamNumber.setText(qnum.toString());
					}
				}
			}
		}

		if (Integer.parseInt(txtTotalPoints.getText()) == 100) {
			btnAddQuestion.setEnabled(false);
			btnCreateNewExam.setEnabled(true);
		}

	}
/**
 * Restore everything To Default
 */
	public void RestoreToDefualt() {
		cboxQstSelect.removeAllItems();
		cboxCourse.removeAllItems();
		cboxCourse.setEnabled(false);
		cboxSubject.setEnabled(true);
		cboxQstSelect.setEnabled(false);

		examQuestions.clear();

		sldExamTime.setValue(180);

		txtPoint.setEnabled(false);
		txtExamNumber.setText("0");
		txtPoint.setText("0");
		txtQstPreview.setText(" ");
		txtLecturerComments.setText("Lecturer comments");
		txtStudentComments.setText("Student comments");
		txtTotalPoints.setText("0");
		txtQCount.setText("0");

		btnAddQuestion.setEnabled(false);
		btnCreateNewExam.setEnabled(false);

	}
/**
 * enable Texts -Impossible to write 
 */
	private void enableTxts() {
		cboxCourse.setEnabled(true);
		cboxQstSelect.setEnabled(true);
		txtLecturerComments.setEnabled(true);
		txtStudentComments.setEnabled(true);
		txtPoint.setEnabled(true);
	}
/**
 * add Subjects to combo
 * @param sbj list of Subjects
 */
	public void addSubjects(ArrayList<Subject> sbj) {
		this.subjectLst = new ArrayList<Subject>();
		cboxSubject.removeAllItems();
		this.subjectLst.addAll(0, sbj);
		for (int i = 0; i < (sbj.size()); i++)
			cboxSubject.addItem(subjectLst.get(i).getSubjectID() + " : "
					+ sbj.get(i).getSubjectName());
		cboxSubject.addActionListener(this);
		cboxCourse.addActionListener(this);
		cboxQstSelect.addActionListener(this);

	}
/**
 * add Course to combo 
 * @param lstCrs Course list
 */
	public void addCourses(ArrayList<Course> lstCrs) {
		this.courseLst = new ArrayList<Course>();
		this.courseLst.addAll(0, lstCrs);
		for (int i = 0; i < (lstCrs.size()); i++) {
			cboxCourse.addItem(courseLst.get(i).getCourseID() + " : "
					+ lstCrs.get(i).getCourseName());
		}

	}

	@Override
	public void stateChanged(ChangeEvent e) {
		if (e.getSource() == sldExamTime) {
			Integer tmp = new Integer(sldExamTime.getValue());
			lblExamTime.setText(tmp.toString());
		}
	}

	public void addQuestions(ArrayList<Question> lstQst) {
		this.questionLst = new ArrayList<Question>();
		this.questionLst.addAll(0, lstQst);
		for (int i = 0; i < (lstQst.size()); i++)
			cboxQstSelect.addItem(questionLst.get(i).getSubjectID()
					+ questionLst.get(i).getqID().toString());
	}

	@Override
	public void focusGained(FocusEvent e) {
		if (e.getSource() == txtLecturerComments) {
			if (txtLecturerComments.getText().equals("Lecturer comments"))
				txtLecturerComments.setText("");
		}
		if (e.getSource() == txtStudentComments) {
			if (txtStudentComments.getText().equals("Student comments"))
				txtStudentComments.setText("");
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
		if (e.getSource() == txtLecturerComments) {
			if (txtLecturerComments.getText().equals(""))
				txtLecturerComments.setText("Lecturer comments");
		}
		if (e.getSource() == txtStudentComments) {
			if (txtStudentComments.getText().equals(""))
				txtStudentComments.setText("Student comments");
		}

	}

	public Course getSelectedCourse() {
		return courseLst.get(cboxCourse.getSelectedIndex());
	}

	public Subject getSelectedSubject() {

		return subjectLst.get(cboxSubject.getSelectedIndex());
	}

	public Integer getExamID() {

		return Integer.parseInt(txtExamNumber.getText());
	}

	public Date getDurationTime() {
		int time = Integer.parseInt(lblExamTime.getText());
		time = time * 60000;
		return new Date(time);
	}

	public String getLecturerComment() {
		if (txtLecturerComments.equals("Lecturer comments"))
			return " ";

		return txtLecturerComments.getText();

	}

	public String getStudentComment() {
		if (txtLecturerComments.equals("Student comments"))
			return " ";

		return txtStudentComments.getText();
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		

	}

	@Override
	public void keyReleased(KeyEvent e) {
		if ((e.getSource() == txtPoint) && !(e.getKeyChar() == '\n')) {
			if (!Character.isDigit(e.getKeyChar()))
				txtPoint.setText("");
			if (!txtPoint.getText().isEmpty()) {
				Integer totalPoints = new Integer(
						Integer.parseInt(txtTotalPoints.getText())
								+ Integer.parseInt(txtPoint.getText()));
				if (totalPoints > 100) {
					Perror.pError("Total points for the exam will exceed 100!");
					txtPoint.grabFocus();
					btnAddQuestion.setEnabled(false);
				} else if (Integer.parseInt(txtPoint.getText()) == 0)
					;
				else
					btnAddQuestion.setEnabled(true);
			}
			else
			btnAddQuestion.setEnabled(false);
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		

	}
}
